from colorama import Fore, Style  # 控制台颜色输出
from getpass import getpass  # 遮挡密码
from service.user_service import UserService
from service.news_service import NewService
from service.role_service import RoleService
import os
import sys
import time

__user_service = UserService()
__news_service = NewService()
__role_service = RoleService()

while True:
    os.system("cls")  # 清屏
    # 设置登陆界面样式
    print(Fore.LIGHTBLUE_EX, "\n\t=============================")
    print(Fore.LIGHTBLUE_EX, "\n\t欢迎使用新闻管理系统")
    print(Fore.LIGHTBLUE_EX, "\n\t=============================")
    print(Fore.LIGHTGREEN_EX, "\n\t1.登录系统")
    print(Fore.LIGHTGREEN_EX, "\n\t2.退出系统")

    # 用户选择操作编号，清楚颜色样式 操作编号
    print(Style.RESET_ALL)
    operation_number = input("\n\t输入操作编号：")

    if operation_number == '1':
        username = input("\n\t请输入您的用户名：")
        password = getpass("\n\t请输入您的密码：")  # 在虚拟端不可用

        login_result = __user_service.login(username, password)
        # print(login_result)
        # 登录成功：
        if login_result:
            # 查询角色
            role = __user_service.search_user_role(username)

            os.system("cls")

            while True:
                if role == "新闻编辑":
                    pass
                elif role == "管理员":
                    print(Fore.LIGHTGREEN_EX, "\n\t1.新闻管理")
                    print(Fore.LIGHTGREEN_EX, "\n\t2.用户管理")
                    print(Fore.LIGHTRED_EX, "\n\tback.退出登录")
                    print(Fore.LIGHTRED_EX, "\n\texit.退出系统")
                    print(Style.RESET_ALL)

                    operation_number = input("\n\t输入操作编号：")

                    # 新闻管理
                    if operation_number == '1':
                        while True:
                            os.system("cls")
                            print(Fore.LIGHTGREEN_EX, "\n\t1.审批新闻")
                            print(Fore.LIGHTGREEN_EX, "\n\t2.删除新闻")
                            print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
                            print(Style.RESET_ALL)
                            operation_number = input("\n\t输入操作编号：")

                            if operation_number == '1':
                                # 默认第一页
                                page = 1
                                while True:
                                    os.system("cls")
                                    count_page = __news_service.pending_news_count_page()
                                    news_list = __news_service.approval_news_list(page)

                                    for index in range(len(news_list)):
                                        one_record = news_list[index]
                                        print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s\t%s" % (
                                            index + 1, one_record[1], one_record[2], one_record[3]))
                                    print(Fore.LIGHTGREEN_EX, "\n\t--------------------")
                                    print(Fore.LIGHTGREEN_EX, "\n\t%d/%d" % (page, count_page))
                                    print(Fore.LIGHTGREEN_EX, "\n\t--------------------")
                                    print(Fore.LIGHTGREEN_EX, "\n\tback.返回上一层")
                                    print(Fore.LIGHTGREEN_EX, "\n\tprev.上一页")
                                    print(Fore.LIGHTGREEN_EX, "\n\tnext.下一页")
                                    print(Style.RESET_ALL)
                                    operation_number = input("\n\t输入操作编号：")

                                    if operation_number == "back":
                                        break
                                    elif operation_number == "prev" and page > 1:
                                        page -= 1
                                    elif operation_number == "next" and page < count_page:
                                        page += 1
                                    elif 1 <= int(operation_number) <= 10:
                                        news_id = news_list[int(operation_number) - 1][0]
                                        __news_service.approval_news(news_id)
                            elif operation_number == '2':
                                # 默认第一页
                                page = 1
                                while True:
                                    os.system("cls")
                                    count_page = __news_service.query_total_number_news()
                                    news_list = __news_service.query_news_list(page)

                                    for index in range(len(news_list)):
                                        one_record = news_list[index]
                                        print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s\t%s" % (
                                            index + 1, one_record[1], one_record[2], one_record[3]))
                                    print(Fore.LIGHTGREEN_EX, "\n\t--------------------")
                                    print(Fore.LIGHTGREEN_EX, "\n\t%d/%d" % (page, count_page))
                                    print(Fore.LIGHTGREEN_EX, "\n\t--------------------")
                                    print(Fore.LIGHTGREEN_EX, "\n\tback.返回上一层")
                                    print(Fore.LIGHTGREEN_EX, "\n\tprev.上一页")
                                    print(Fore.LIGHTGREEN_EX, "\n\tnext.下一页")
                                    print(Style.RESET_ALL)
                                    operation_number = input("\n\t输入操作编号：")

                                    if operation_number == "back":
                                        break
                                    elif operation_number == "prev" and page > 1:
                                        page -= 1
                                    elif operation_number == "next" and page < count_page:
                                        page += 1
                                    elif 1 <= int(operation_number) <= 10:
                                        news_id = news_list[int(operation_number) - 1][0]
                                        __news_service.delete_new_byid(news_id)
                            elif operation_number == "back":
                                os.system("cls")
                                break
                    #  用户管理
                    elif operation_number == '2':
                        while True:
                            os.system("cls")
                            print(Fore.LIGHTGREEN_EX, "\n\t1.添加用户")
                            print(Fore.LIGHTGREEN_EX, "\n\t2.修改用户")
                            print(Fore.LIGHTGREEN_EX, "\n\t3.删除用户")
                            print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
                            print(Style.RESET_ALL)
                            operation_number = input("\n\t输入操作编号：")

                            if operation_number == "back":
                                break
                            elif operation_number == '1':
                                os.system("cls")
                                username = input("\n\t请输入用户名：")
                                password = getpass("\n\t请输入密码：")
                                repassword = getpass("\n\t请再次输入密码：")

                                if password != repassword:
                                    print("\n\t两次密码不一致(3秒自动返回)")
                                    continue
                                email = input("\n\t请输入邮箱：")

                                role_list = __role_service.query_role_list()

                                for index in range(len(role_list)):
                                    role_record = role_list[index]
                                    print(Fore.LIGHTBLUE_EX, "\n\t%d.%s" % (index + 1, role_record[1]))

                                print(Style.RESET_ALL)
                                operation_number = input("\n\t角色编号：")
                                role_id = role_list[int(operation_number) - 1][0]

                                __user_service.add_user(username, password, email, role_id)
                                print("\n\t添加用户成功(3秒自动返回)")
                                time.sleep(3)
                            elif operation_number == '2':
                                page = 1
                                while True:
                                    os.system("cls")
                                    count_page = __user_service.query_total_number_users()
                                    users_list = __user_service.user_pagination_record(page)

                                    for index in range(len(users_list)):
                                        user_record = users_list[index]
                                        print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s" % (
                                            index + 1, user_record[1], user_record[2]))
                                    print(Fore.LIGHTGREEN_EX, "\n\t--------------------")
                                    print(Fore.LIGHTGREEN_EX, "\n\t%d/%d" % (page, count_page))
                                    print(Fore.LIGHTGREEN_EX, "\n\t--------------------")
                                    print(Fore.LIGHTGREEN_EX, "\n\tback.返回上一层")
                                    print(Fore.LIGHTGREEN_EX, "\n\tprev.上一页")
                                    print(Fore.LIGHTGREEN_EX, "\n\tnext.下一页")
                                    print(Style.RESET_ALL)

                                    operation_number = input("\n\t输入操作编号：")
                                    if operation_number == "back":
                                        break
                                    elif operation_number == "prev" and page > 1:
                                        page -= 1
                                    elif operation_number == "next" and page < count_page:
                                        page += 1
                                    elif 1 <= int(operation_number) <= 10:
                                        os.system("cls")
                                        user_id = users_list[int(operation_number) - 1][0]
                                        username = input("\n\t请输入新用户名：")
                                        password = getpass("\n\t请输入新密码：")
                                        repassword = getpass("\n\t请再次输入新密码：")

                                        if password != repassword:
                                            print(Fore.LIGHTRED_EX, "\n\t两次密码不一致(3秒自动返回)")
                                            print(Style.RESET_ALL)
                                            time.sleep(3)
                                        email = input("\n\t请输入邮箱：")
                                        role_list = __role_service.query_role_list()

                                        for index in range(len(role_list)):
                                            role_record = role_list[index]
                                            print(Fore.LIGHTBLUE_EX, "\n\t%d.%s" % (index + 1, role_record[1]))

                                        print(Style.RESET_ALL)
                                        operation_number = input("\n\t角色编号：")
                                        role_id = role_list[int(operation_number) - 1][0]

                                        operation = input("\n\t是否保存(Y/N)")
                                        if operation.upper() == 'Y':
                                            __user_service.update_user(user_id, username, password, email, role_id)
                                            print("\n\t修改用户成功(3秒自动返回)")
                                            time.sleep(3)
                                        elif operation.upper() == 'N':
                                            break
                            elif operation_number == '3':
                                page = 1
                                while True:
                                    os.system("cls")
                                    count_page = __user_service.query_total_number_users()
                                    users_list = __user_service.user_pagination_record(page)

                                    for index in range(len(users_list)):
                                        user_record = users_list[index]
                                        print(Fore.LIGHTBLUE_EX, "\n\t%d\t%s\t%s" % (
                                            index + 1, user_record[1], user_record[2]))
                                    print(Fore.LIGHTGREEN_EX, "\n\t--------------------")
                                    print(Fore.LIGHTGREEN_EX, "\n\t%d/%d" % (page, count_page))
                                    print(Fore.LIGHTGREEN_EX, "\n\t--------------------")
                                    print(Fore.LIGHTGREEN_EX, "\n\tback.返回上一层")
                                    print(Fore.LIGHTGREEN_EX, "\n\tprev.上一页")
                                    print(Fore.LIGHTGREEN_EX, "\n\tnext.下一页")
                                    print(Style.RESET_ALL)

                                    operation_number = input("\n\t输入操作编号：")
                                    if operation_number == "back":
                                        break
                                    elif operation_number == "prev" and page > 1:
                                        page -= 1
                                    elif operation_number == "next" and page < count_page:
                                        page += 1
                                    elif 1 <= int(operation_number) <= 10:
                                        os.system("cls")
                                        user_id = users_list[int(operation_number) - 1][0]
                                        __user_service.delete_user(user_id)

                                        print("\n\t删除用户成功(3秒自动返回)")
                                        time.sleep(3)
                    elif operation_number == "back":
                        break
                    elif operation_number == "exit":
                        sys.exit(0)

        # 登录失败
        else:
            print("\n\t登陆失败(3秒自动返回)")
            time.sleep(3)
    elif operation_number == '2':
        # 退出系统
        sys.exit(0)
